package com.apes.fn.scm.commodityInquiry.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Map;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ExternalInquiryItem.CONST_TABLE_NAME)
public class ExternalInquiryItem extends BaseModel {

    public static final String CONST_TABLE_NAME = "pur_external_inquiry_item";


    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name="商品询价明细", prefix = "1")
    @Comment("编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "external_inquiry_id", referencedColumnName = "id", nullable = false)
    @Comment("询价单号")
    private ExternalInquiry externalInquiry;

    @Column(name = "map_address", length = 4000)
    @Comment("商品图片")
    private String mapAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    @Comment("配件")
    private Category category;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    @Comment("商品编码")
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id", nullable = false)
    @Comment("计量单位")
    private ProductUom uom;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id", nullable = false)
    private Location location;  //仓库库区

    @Transient
    private Map uomBase;
    public Map getUomBase(){
        if (uom == null) return null;
        return MapUtil.mapper(
                "JLDW", uom.getName(),
                "HSBL", uom.getFactor()
        );
    }

    @Column(name = "sale_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double saleQty = 0;

    @Column(name = "purchase_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("采购单价")
    private double purchaseUnit;

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售单价")
    private double priceUnit;

    @Column(name = "price_limit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售限价")
    private double priceLimit;

    @Column(name = "down_company_purchase_price", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("下游公司采购价")
    private double downCompanyPurchasePrice;

    @Column(name = "inquiry_reason", length = 4000)
    @Comment("询价原因")// 旧
    private String inquiryReason;

    @Comment("询价原因类型" ) // 新
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inquiry_reason_type", referencedColumnName = "id" )
    private Enumeration inquiryReasonType ;

    @Column(name = "dept_remark", length = 4000)
    @Comment("门店备注")
    private String deptRemark;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_quality", referencedColumnName = "id")
    @Comment("外采商品品质")
    private Enumeration productQuality;

    @Comment("外采商品品牌")
    @Column(name = "product_brand", length = 4000)
    private String productBrand;

    @Column(name = "purchase_remark", length = 4000)
    @Comment("采购备注")
    private String purchaseRemark;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "settlement_type", referencedColumnName = "id")
    @Comment("结算方式（month：月结，halfMonth：半月结，week：周结，bill：账单，totalCash：总部现金，deptCash：门店现金）")
    private Enumeration settlementType;

    @Column(name = "product_rule", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("带安标记")
    private boolean productRule;

    @States(name = "询价勾选标记", values = {
            @State(id = "false", name = "不选择"),
            @State(id = "true", name = "选择")
    })
    @Column(name = "inquiry_select_state", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    private boolean inquirySelectState = false;

    @Column(name = "valid_period")
    @Comment("有效期止")
    private Date validPeriod;


    /***
     * 询过价/有库存/有要货
     */
    @Column(name = "refer", length = 4000)
    @Comment("参考信息")
    private String refer;

    public void addRefer(String refer) {
        if (this.refer == null) {
            this.refer = refer;
            return;
        }

        this.refer += ("/" + refer);
    }
}
